package com.yx.cloud.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;

import com.iot.base.service.req.NetworkConfigParam;
import com.iot.common.utils.DensityUtil;
import com.iot.common.utils.LogUtils;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.SelectseriesActivityBinding;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.SharedPrefsUtil;

/**
 * 机器系列选择界面
 */
public class SelectSeriesActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SelectSeriesActivity";
    private SelectseriesActivityBinding binding;
    private NetworkConfigParam param;
    private String EZandAP = "";
    private boolean visibility;
    private String selectseries;
    private String robotType;
    private int selectriesPicId;

    @Override
    public void onCreate() {
        binding = SelectseriesActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();
    }

    private void initData() {
        //默认配网模式
        param = new NetworkConfigParam();
        //TODO 通过config获取支持的配网方式
        EZandAP = "AP";
        SharedPrefsUtil.putValue(this, "EZandAP", "EZandAP", EZandAP);
        robotType = getIntent().getStringExtra("device_type");
        LogUtils.i("------------------------> " + robotType);
        if (robotType.equals("u3-full")) {
            visibility = false;
            selectseries = "开机后，长按电源键和回充键3S，听到“Wi-Fi已重置”提示音后，确认Wi-Fi灯在快闪";
            selectriesPicId = R.mipmap.demonstration_icon_u3pro;
            setUiInitialize(visibility, selectseries, robotType, selectriesPicId);
        } else {
            visibility = false;
            selectseries = getString(R.string.N_Guide_Text_X3);
            selectriesPicId = R.mipmap.demonstration_icon_inxni;
            setUiInitialize(visibility, selectseries, robotType, selectriesPicId);
        }
        //跳槽免责声明弹窗
        policyPrivacyDialog();
//        Intent intent = new Intent(this, DisclaimerDialog.class);
//        startActivityForResult(intent, 2);

        binding.btnSelectSeriesButton.setEnabled(false);
        binding.cbConfirmLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    binding.btnSelectSeriesButton.setEnabled(true);
                } else {
                    binding.btnSelectSeriesButton.setEnabled(false);
                }
            }
        });
        binding.ivSelectSeriesReturn.setOnClickListener(this);
        binding.btnSelectSeriesButton.setOnClickListener(this);
        binding.tvSeeHelp.setOnClickListener(this);
        binding.ivEzAndAp.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_selectSeries_return) {
            Intent_utils.backIntent(this);
        } else if (id == R.id.tv_seeHelp) {
            //配网帮助
            Intent_utils.enterIntentContext(this, ExamineHelpActivity.class, "device_type", robotType);
        } else if (id == R.id.btn_selectSeriesButton) {
            String value = SharedPrefsUtil.getValue(SelectSeriesActivity.this, "EZandAP", "EZandAP", "");
            if ("QR".equals(value)) {
                Intent select = new Intent(this, SelectWIFIActivity.class);
                select.putExtra("network", "qrcode");
                select.putExtra("source", "other");
                startActivity(select);
            } else {
                Intent intent = new Intent(this, SelectWIFIActivity.class);
                intent.putExtra("source", "other");
                intent.putExtra("network", "wifi");
                intent.putExtra("device_type", robotType);
                startActivity(intent);
            }
            finish();
        } else if (id == R.id.iv_ezAndAp) {
            showPopupWindow();
        }
    }

    private void showPopupWindow() {

        View inflate = LayoutInflater.from(this).inflate(R.layout.popup_process, null);
        PopupWindow popupWindow = new PopupWindow(inflate, GridLayout.LayoutParams.WRAP_CONTENT, GridLayout.LayoutParams.WRAP_CONTENT);
        //设置popupWindow中的item可以被点击，这句话是必须要添加的
        popupWindow.setFocusable(true);
        //设置PopupWindow的背景
        //如果不设置背景，会导致无论是点击外部区域还是Back键都无法dismiss掉popupWindow
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        popupWindow.setBackgroundDrawable(dw);

        int[] location = new int[2];
        binding.ivEzAndAp.getLocationOnScreen(location);//得到相对当前控件的坐标位置
        //设置popupWindow显示的位置
        popupWindow.showAsDropDown(binding.ivEzAndAp);

        LinearLayout ll_ezNetwork = inflate.findViewById(R.id.ll_ezNetwork);
        LinearLayout ll_apNetwork = inflate.findViewById(R.id.ll_apNetwork);
        LinearLayout ll_qrNetwork = inflate.findViewById(R.id.ll_qrNetwork);
        ImageView iv_ezNetwork = inflate.findViewById(R.id.iv_ezNetwork);
        ImageView iv_apNetwork = inflate.findViewById(R.id.iv_apNetwork);
        ImageView iv_qrNetwork = inflate.findViewById(R.id.iv_qrNetwork);

        TextView tv_ezNetwork = inflate.findViewById(R.id.tv_ezNetwork);
        TextView tv_apNetwork = inflate.findViewById(R.id.tv_apNetwork);
        TextView tv_qrNetwork = inflate.findViewById(R.id.tv_qrNetwork);

//        if (ZW2X.get().getConfig().productName.equals(AppContext.get().getRobotType())) {
//            //显示AP 二维码
//            netWorkModeShow(ll_ezNetwork, false, ll_apNetwork, true, ll_qrNetwork, false);
//        } else if (RS11.get().getConfig().productName.equals(AppContext.get().getRobotType())
//                || GD01.get().getConfig().productName.equals(AppContext.get().getRobotType())
//                || X1.get().getConfig().productName.equals(AppContext.get().getRobotType())
//                || RS01.get().getConfig().productName.equals(AppContext.get().getRobotType())
//        ) {
//            //显示AP EZ
//            netWorkModeShow(ll_ezNetwork, true, ll_apNetwork, true, ll_qrNetwork, false);
//        } else {
//            //显示AP
//            netWorkModeShow(ll_ezNetwork, false, ll_apNetwork, true, ll_qrNetwork, false);
//        }
        netWorkModeShow(ll_ezNetwork, false, ll_apNetwork, true, ll_qrNetwork, false);
        String value = SharedPrefsUtil.getValue(SelectSeriesActivity.this, "EZandAP", "EZandAP", "");
        if (value.equals("EZ")) {
            iv_ezNetwork.setVisibility(View.VISIBLE);
            iv_apNetwork.setVisibility(View.INVISIBLE);
            iv_qrNetwork.setVisibility(View.INVISIBLE);
            tv_ezNetwork.setText(getString(R.string.N_Network_Ez));
        } else if (value.equals("AP")) {
            iv_ezNetwork.setVisibility(View.INVISIBLE);
            iv_apNetwork.setVisibility(View.VISIBLE);
            iv_qrNetwork.setVisibility(View.INVISIBLE);
            tv_apNetwork.setText(getString(R.string.N_Network_Ap));
        } else if (value.equals("QR")) {
            iv_ezNetwork.setVisibility(View.INVISIBLE);
            iv_apNetwork.setVisibility(View.INVISIBLE);
            iv_qrNetwork.setVisibility(View.VISIBLE);
            tv_qrNetwork.setText(getString(R.string.QR_code_distribution_network));
        }

        ll_ezNetwork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                SharedPrefsUtil.putValue(SelectSeriesActivity.this, "EZandAP", "EZandAP", "EZ");
                iv_ezNetwork.setVisibility(View.VISIBLE);
                iv_apNetwork.setVisibility(View.INVISIBLE);
                iv_qrNetwork.setVisibility(View.INVISIBLE);
                binding.tvSelectseries.setText(R.string.N_Guide_Text_X3);
//                param.setModel(ActivatorModelEnum.TY_EZ);
            }
        });
        ll_apNetwork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                SharedPrefsUtil.putValue(SelectSeriesActivity.this, "EZandAP", "EZandAP", "AP");
                iv_ezNetwork.setVisibility(View.INVISIBLE);
                iv_apNetwork.setVisibility(View.VISIBLE);
                iv_qrNetwork.setVisibility(View.INVISIBLE);
//                param.setModel(ActivatorModelEnum.TY_AP);
                binding.tvSelectseries.setText(R.string.N_Guide_Text_X3);
            }
        });
        ll_qrNetwork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                SharedPrefsUtil.putValue(SelectSeriesActivity.this, "EZandAP", "EZandAP", "QR");
                iv_ezNetwork.setVisibility(View.INVISIBLE);
                iv_apNetwork.setVisibility(View.INVISIBLE);
                iv_qrNetwork.setVisibility(View.VISIBLE);
                binding.tvSelectseries.setText(R.string.N_Guide_Text_X3);
            }
        });
    }

    private void netWorkModeShow(View ez, boolean ezVisible, View ap, boolean apVisible, View qr, boolean qrVisible) {
        ez.setVisibility(ezVisible ? View.VISIBLE : View.GONE);
        ap.setVisibility(apVisible ? View.VISIBLE : View.GONE);
        qr.setVisibility(qrVisible ? View.VISIBLE : View.GONE);
    }

    private void setUiInitialize(boolean visible, String strId, String robotype, int selectriesPicId) {
        binding.ivEzAndAp.setVisibility(visible ? View.VISIBLE : View.GONE);
        binding.tvSelectseries.setText(strId);
        binding.tvSelectSeriesTitle.setText(ProductConfigConstants.getNetworkDeviceTypeName(SelectSeriesActivity.this, robotype));
        binding.ivSelectriesPic.setBackgroundResource(selectriesPicId);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            if (resultCode == 3) {
                boolean tag = data.getBooleanExtra("tag", false);
                if (!tag) {
                    Intent_utils.backIntent(this);
                }
            }
        }
    }


    private void policyPrivacyDialog() {
        Dialog dialog = new Dialog(SelectSeriesActivity.this);
        dialog.setContentView(R.layout.disclaimer_dialog);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        TextView mTvConsent = dialog.findViewById(R.id.btn_agree);
        TextView mTvReject = dialog.findViewById(R.id.btn_reject);
        TextView mTvDisclaimerServer = dialog.findViewById(R.id.tv_disclaimerServer);
        TextView mTvDisclaimerPrivacy = dialog.findViewById(R.id.tv_disclaimerPrivacy);
        ScrollView dialogScrollView = dialog.findViewById(R.id.dialog_scrollView);
        LinearLayout llScrollViewLayout = dialog.findViewById(R.id.ll_scrollViewLayout);

        mTvDisclaimerServer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                Intent_utils.enterIntentContext(SelectSeriesActivity.this, ServerAndPrivacyActivity.class, "Server_Privacy", "Server");
            }
        });
        mTvDisclaimerPrivacy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                Intent_utils.enterIntentContext(SelectSeriesActivity.this, ServerAndPrivacyActivity.class, "Server_Privacy", "Privacy");
            }
        });


        mTvConsent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                SharedPrefsUtil.putValue(SelectSeriesActivity.this, "ServerAndPrivacySign", "ServerAndPrivacySign", true);
                dialog.dismiss();
            }
        });
        mTvReject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                SharedPrefsUtil.putValue(SelectSeriesActivity.this, "ServerAndPrivacySign", "ServerAndPrivacySign", false);
                dialog.dismiss();
                Intent_utils.backIntent(SelectSeriesActivity.this);
            }
        });
        dialog.show();
    }

}
